# coding: utf-8

from django.db import models

class MainMixin(object):
    url_prefix = None
    def get_list_url(self): return '/{0}/'.format(self.url_prefix)
    def get_absolute_url(self): return '/{0}/{1}'.format(self.url_prefix, self.pk)
    def get_add_url(self): return '/{0}/add'.format(self.url_prefix)
    def get_edit_url(self): return '/{0}/edit/{1}'.format(self.url_prefix, self.pk)
    def get_delete_url(self): return '/{0}/del/'.format(self.url_prefix)

# Create your models here.
class Computer(models.Model, MainMixin):
    url_prefix = 'network'
    IP_ADDRESS_TYPES = (('dhcp','Автоматически'), ('manual', 'Вручную'))
    name = models.CharField(u'Имя компьютера', max_length=64)
    ip_address = models.IPAddressField(u'IP-адрес', blank = True, null = True)
    ip_type = models.CharField(u'IP-адрес назначен', max_length=16, choices=IP_ADDRESS_TYPES)
    departament = models.ForeignKey('Departament', verbose_name = u'Отдел')
    inv_number = models.CharField(u'Инвентарный номер', max_length = 32)
    def __unicode__(self):
        return u"{0} {1} ({2})".format(self.name, self.ip_address, self.ip_type)
    class Meta:
        ordering = ['name']
        verbose_name = u'Компьютер'
        verbose_name_plural = u'Компьютеры'

class Departament(models.Model, MainMixin):
    url_prefix = 'depart'
    name = models.CharField(u'Название отдела', max_length=256)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ['name']
        verbose_name = u'Отдел'
        verbose_name_plural = u'Отделы'

class ComputerUser(models.Model, MainMixin):
    url_prefix = 'users'
    name = models.CharField(u'Имя', max_length=256)
    login = models.CharField(u'Логин', max_length=256)
    departament = models.ForeignKey(Departament)
    computer = models.ManyToManyField(Computer)
    def __unicode__(self):
        return u"{0} ({1})".format(self.name, self.departament)
    class Meta:
        ordering = ['name']
        verbose_name = u'Пользователь'
        verbose_name_plural = u'Пользователи'
